package utils;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import aw.Static_Variables;

public class ReadXMLFile {
	
	public ReadXMLFile(){}
	
	public World createMap() {
		
		World world = null;

		try {
			File fXmlFile = new File("Map2.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);

			doc.getDocumentElement().normalize();

			//System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
			
			Node worldNode = doc.getDocumentElement();
			int nRooms = Integer.parseInt(worldNode.getAttributes().getNamedItem("numberOfRooms").getNodeValue());
			int xMax = Integer.parseInt(worldNode.getAttributes().getNamedItem("xMax").getNodeValue());
			int yMax = Integer.parseInt(worldNode.getAttributes().getNamedItem("yMax").getNodeValue());
			
			world = new World(nRooms, xMax, yMax);
			
			NodeList nList = doc.getElementsByTagName("room");

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;
					int roomId = Integer.parseInt(eElement.getAttribute("id"));
					NamedNodeMap firstPos = eElement.getElementsByTagName("firstPosition").item(0).getAttributes();
					NamedNodeMap lastPos = eElement.getElementsByTagName("lastPosition").item(0).getAttributes();
					NodeList acessories = eElement.getElementsByTagName("object");
					NodeList doors = eElement.getElementsByTagName("doorsId");
					ArrayList<Acessory> acessoriesArray = new ArrayList<Acessory>();
					
					int firstPosX = Integer.parseInt(firstPos.getNamedItem("x").getNodeValue());
					int firstPosY = Integer.parseInt(firstPos.getNamedItem("y").getNodeValue());
					
					int lastPosX = Integer.parseInt(lastPos.getNamedItem("x").getNodeValue());
					int lastPosY = Integer.parseInt(lastPos.getNamedItem("y").getNodeValue());
					
					for (int i = 0; i< acessories.getLength(); i++) {
						Element auxAcessory = (Element) acessories.item(i);
						String id = auxAcessory.getAttribute("id");
						int posX = Integer.parseInt(auxAcessory.getAttribute("x"));
						int posY = Integer.parseInt(auxAcessory.getAttribute("y"));
						//System.out.println("objecto: " + id + " " + posX + " " + posY);
						Acessory ac = new Acessory(id, posX, posY,0);
						acessoriesArray.add(ac);
					}
					
					String doorsString = doors.item(0).getAttributes().getNamedItem("ids").getNodeValue();
					
					//System.out.println(roomId+" "+ firstPosX+" "+ firstPosY+" "+ lastPosX+" "+ lastPosY+" "+ acessoriesArray+" "+ doorsString);
					Room room = new Room(roomId, firstPosX, firstPosY, lastPosX, lastPosY, acessoriesArray, doorsString);
					
					world.setRooms(roomId, room);
				}
			}
			
			
			NodeList nListDoors = doc.getElementsByTagName("doors");

			for (int temp = 0; temp < nListDoors.getLength(); temp++) {
				
				Node nNode = nListDoors.item(temp);
				
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;
					NodeList doors = eElement.getElementsByTagName("door");
					
					
					for (int i = 0; i< doors.getLength(); i++) {
						Element auxDoor = (Element) doors.item(i);
						int id = Integer.parseInt(auxDoor.getAttribute("id"));
						String pos1 = auxDoor.getAttribute("pos1");
						String pos2 = auxDoor.getAttribute("pos2");
						int room1 = Integer.parseInt(auxDoor.getAttribute("room1"));
						int room2 = Integer.parseInt(auxDoor.getAttribute("room2"));
						String state = auxDoor.getAttribute("state");
						Door d = new Door(id, pos1, pos2, room1, room2, state);
						System.out.println("state: "+state);
						Static_Variables.doorsTab.put(id, d);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return world;
	}

}